#!/bin/bash

# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ROOT_DIR=`pwd`

OHOS_SDK=ohos-sdk
SDK_URL="https://cidownload.openharmony.cn/version/Master_Version/OpenHarmony_5.0.0.29_dev/20240628_020211/version-Master_Version-OpenHarmony_5.0.0.29_dev-20240628_020211-ohos-sdk-full.tar.gz"
SDK_SHA512SUM="341b809dac751347173a2a64b45946ba6ba37d433e8fc1551ca5d413d2c16dda38e10b8cf1cfea7c041687e635f7ae34573637fc45289e318eee1d4673801f1a"

function prepare_sdk() {

    echo "start prepare sdk #########"

    if [ -f $OHOS_SDK.tar.gz ]
    then
        local sdk_sum=`sha512sum $OHOS_SDK.tar.gz`
        sdk_sum=${sdk_sum:0:128}
        if [ "x$sdk_sum" != "x$SDK_SHA512SUM" ]
        then
            echo "x$sdk_sum"
            echo "x$SDK_SHA512SUM"
            echo "must change SDK!!"
            rm -rf $OHOS_SDK*
        fi
    fi

    if [ ! -d $OHOS_SDK ]
    then
        if [ ! -f $OHOS_SDK.tar.gz ]
        then
            curl -f -L -- "$SDK_URL" > $OHOS_SDK.tar.gz
            if [ $? -ne 0 ]
            then
                rm -f $OHOS_SDK.tar.gz
                echo "download ohos-sdk failed! #######"
                return 1
            fi
        fi

        mkdir $OHOS_SDK
        tar -zxvf $OHOS_SDK.tar.gz -C $OHOS_SDK
        if [ $? -ne 0 ]
        then
            echo "decompress sdk package failed! #####"
            rm -rf $OHOS_SDK $OHOS_SDK.tar.gz
            return 1
        fi

        cd $OHOS_SDK/linux
        for file in `ls ./`
        do
            unzip $file
        done
        cd native/llvm/bin
        cd $ROOT_DIR
    fi

    return 0
}

cmake_package=cmake-3.26.4-linux-x86_64.tar.gz
cmake_dir=cmake-3.26.4-linux-x86_64
function prepare_cmake() {   # cmake 版本3.26
    echo "start prepare cmake #########"
    if [ ! -d $cmake_dir ]
    then

        if [ ! -f $cmake_package ]
        then
            curl -L https://cmake.org/files/v3.26/cmake-3.26.4-linux-x86_64.tar.gz --output $cmake_package
            if [ $? -ne 0 ]
            then
                rm -f $cmake_package
                echo "download cmake failed!!"
                return 1
            fi
        fi

        tar -zxf $cmake_package
        if [ $? -ne 0 ]
        then
            echo "unpack cmake failed!!!"
            rm -f $cmake_package
            return 1
        fi
    fi

    cp $OHOS_SDK/linux/native/build-tools/cmake/share/cmake-3.16/Modules/Platform/OHOS.cmake $cmake_dir/share/cmake-3.26/Modules/Platform

    echo "cmake prepare success!!"

    return 0
}

function prepare_apt_sources() {
    if grep -q "http://repo.huaweicloud.com/ubuntu" /etc/apt/sources.list;
    then
        cp -a /etc/apt/sources.list /etc/apt/sources.list.org

        source_list="deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse"
        echo "$source_list" > /etc/apt/sources.list
    fi

    apt update

    return 0
}

function prepare_commone_cmd() {
    local commands=("gcc" "make" "pkg-config" "autoconf" "autoreconf" "automake" "patch" "libtool" "autopoint" "gperf")
    for cmd in ${commands[@]}
    do
        which $cmd >> /dev/null
        if [ $? -ne 0 ]
        then
            echo "install $cmd"
            apt install $cmd -y
        fi
    done
}

function update_cmd() {
    local update_cmds=("meson")
    local update_versions=("1.1.1")

    for (( i = 0; i < ${#update_cmds[@]}; i++))
    do
        local cmd=${update_cmds[i]}
        local ver=${update_versions[i]}
        local version=`$cmd --version`

        echo "cmd=$cmd ver=$ver version=$version"

        if [ $? -ne 0 ]
        then
            echo "no $cmd command!!!!"
            continue
        fi

        if [ "$version" != "$ver" ]
        then
            pip3 install $cmd==$ver
        fi
    done

    return 0
}

function prepare_cmd() {
    # 需要长期维护此数组
    echo "start prepare cmd #########"
    local cmd_array=("tcl8.6-dev" "wget" "unzip" "gccgo-go" "flex " "bison" "premake4" "python3" "python3-pip" \
    "ninja-build" "meson" "sox" "gfortran" "subversion" "build-essential" "module-assistant" " gcc-multilib" \
    "g++-multilib" "libltdl7-dev" "cabextract" "libboost-all-dev" "libxml2-utils" "gettext" "libxml-libxml-perl" \
    "libxml2" "libxml2-dev" "libxml-parser-perl" "texinfo" "libtool-bin" "xmlto" "po4a" "yasm" "nasm" "xutils-dev" \
    "libx11-dev" "xtrans-dev" "jq")

    local logfile=./tpc_c_cplusplus/thirdparty/build.log

    echo "######## start install command ##############" >> $logfile
    for cmd in ${cmd_array[@]}
    do
        echo "install $cmd" >> $logfile
        apt install $cmd -y
        if [ $? -ne 0 ]
        then
            echo "install cmd:$cmd failed!" >> $logfile
        fi
    done
    
    echo "check libtool################ " >> $logfile
    which libtool >> $logfile

    return 0
}

function prepare() {

    export PATH=/usr/local/bin:$PATH

    prepare_sdk
    if [ $? -ne 0 ]
    then
        echo "prepare_sdk failed!!"
        return 1
    fi
    prepare_cmake
    if [ $? -ne 0 ]
    then
        echo "prepare_cmake failed!!"
        return 1
    fi
    prepare_apt_sources
    prepare_commone_cmd
    prepare_cmd
    update_cmd
    echo "prepare build success!"
    return 0
}

prepare $@
exit $?

#eof
